﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Frame.Lib.Utilities;
using Frame.Lib.Enums;
using Frame.Lib.Common;


namespace Frame.Lib.Core.Right
{
    public class SystemRightBO
    {
        #region 成员变量
        private ISystemRightDAL right;
        private int ownerType;
        #endregion

        #region 属性
        public RightOwnerType OwnerType
        {
            set
            {
                ownerType = (int)value;
            }
        }
        #endregion

        #region 构造函数
        public SystemRightBO(RightOwnerType ownerType)
        {
            right = RightBase.CreateSystemRight((int)ownerType);
        }
        #endregion

        #region 保存一个权限
        /// <summary>
        /// 保存一个权限
        /// </summary>
        /// <param name="iOwnerID"></param>
        /// <param name="strRightName"></param>
        /// <returns></returns>
        public void SaveRight(int ownerID, SystemRight rightName)
        {
            right.SaveRight(ownerID, rightName.ID);
        }
        #endregion

        #region 保存多个权限
        /// <summary>
        /// 保存多个权限
        /// </summary>
        /// <param name="iOwnerID"></param>
        /// <param name="lstRightName"></param>
        /// <returns></returns>
        public void SaveRight(int iOwnerID, List<SystemRight> lstRightName)
        {
            List<int> lstRightIDs = new List<int>(lstRightName.Count);
            foreach (SystemRight rightName in lstRightName)
            {
                lstRightIDs.Add(rightName.ID);
            }
            right.SaveRight(iOwnerID, lstRightIDs);
        }

        public void SaveRight(int ownerID, List<int> lstRightIDs)
        {
            right.SaveRight(ownerID, lstRightIDs);
        }

        #endregion

        #region 保存多个用户
        /// <summary>
        /// 保存多个用户
        /// </summary>
        /// <param name="lstOwnerID"></param>
        /// <param name="strRightName"></param>
        /// <returns></returns>
        public void SaveRight(List<int> lstOwnerID, SystemRight rightName)
        {
            right.SaveRight(lstOwnerID, rightName.ID);
        }
        #endregion

        #region 获得所有权限
        /// <summary>
        /// 获得所有权限
        /// </summary>
        /// <param name="iOwnerID"></param>
        /// <returns></returns>
        public List<int> GetRightIDs(int iOwnerID)
        {
            List<int> lstRightID = right.GetRightIDs(iOwnerID);
            return lstRightID;
        }
        #endregion

        #region 获得拥有一个权限的所有用户
        /// <summary>
        /// 获得拥有一个权限的所有用户
        /// </summary>
        /// <param name="strRightName"></param>
        /// <returns></returns>
        public List<int> GetOwnerIDs(SystemRight rightName)
        {
            List<int> lstOwnerID = right.GetOwnerIDs(rightName.ID);
            return lstOwnerID;
        }
        #endregion

        #region 判断是否拥有权限
        /// <summary>
        /// 判断是否拥有权限
        /// </summary>
        /// <param name="iOwnerID"></param>
        /// <param name="strRightName"></param>
        /// <returns></returns>
        public bool HaveRight(int iOwnerID, SystemRight rightName)
        {
            bool blnFlag = right.HaveRight(iOwnerID, rightName.ID);
            return blnFlag;
        }
        #endregion

        /// <summary>
        /// 获得所有权限类型和名称的矩阵
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllRightRect()
        {
            DataTable dt = new DataTable();
            List<EnumInfo> enumType = ConvertUtil.ConvertEnum(typeof(SystemRightType));//作行头
            List<EnumInfo> enumName = ConvertUtil.ConvertEnum(typeof(SystemRightName));//作列头

            dt.Columns.Add("权限类型");
            foreach (EnumInfo info in enumName)
            {
                dt.Columns.Add(info.Name);
            }

            foreach (EnumInfo type in enumType)
            {
                DataRow row = dt.NewRow();
                row[0] = type.Name;
                foreach (EnumInfo name in enumName)
                {
                    SystemRight right = new SystemRight((SystemRightType)type.ID, (SystemRightName)name.ID);

                    row[name.Name.ToString()] = "<input id='_" + right.ID + "' type='checkbox' onclick='setRight(" + right.ID + ")' /><label for='_" + right.ID + "'>" + name.Name + "</label>";
                }
                dt.Rows.Add(row);
            }

            return dt;
        }

        public static DataTable GetRightRect()
        {
            DataTable dt = new DataTable();
            List<EnumInfo> enumType = ConvertUtil.ConvertEnum(typeof(SystemRightType));//作行头
            List<EnumInfo> enumName = ConvertUtil.ConvertEnum(typeof(SystemRightName));//作列头

            dt.Columns.Add("权限类型");
            foreach (EnumInfo name in enumName)
            {
                dt.Columns.Add(name.Name);
            }
            string strRightIDs = GetRightSet();

            int maxColumns = 0;//最多的一行权限有几列
            foreach (EnumInfo type in enumType)
            {
                DataRow row = dt.NewRow();
                row[0] = type.Name;

                int colIndex = 1;//把每一行的权限靠左对齐
                foreach (EnumInfo name in enumName)
                {
                    SystemRight right = new SystemRight((SystemRightType)type.ID, (SystemRightName)name.ID);
                    if (strRightIDs.Contains("," + right.ID + ","))
                    {
                        row[colIndex] = "<input id='_" + right.ID + "' type='checkbox' onclick='setRight(" + right.ID + ")' /><label for='_" + right.ID + "'>" + name.Name + "</label>";
                        colIndex++;
                    }
                }

                maxColumns = Math.Max(maxColumns, colIndex);

               
                dt.Rows.Add(row);

            }
            //去掉多余的列
            while (dt.Columns.Count > maxColumns)
            {
                dt.Columns.RemoveAt(maxColumns);
            }
            

            return dt;
        }


        public static string GetRightSet()
        {
            return SystemRightDAL.GetRightSet();
        }

        public static void SaveRightSet(string ids)
        {
            SystemRightDAL.SaveRightSet(ids);
        }

        public static void RegisterRightHiddenField()
        {
            ((System.Web.UI.Page)System.Web.HttpContext.Current.CurrentHandler).ClientScript.RegisterHiddenField("hidRightID", "");
        }
    }
}
